<?php

namespace App\Tool\SMS;

use App\Tool\SMS\CCPRestSmsSDK;



class SendTemplateSMS
{
    //主帐号
    private $accountSid='aaf98f8954939ed501549e3b821d0df8';

    //主帐号Token
    private $accountToken='a90a270c524646a8a768ec1ce46f01bc';

    //应用Id
    private $appId='8a48b5515493a1b701549e3bc6190cd7';

    //请求地址，格式如下，不需要写https://
    private $serverIP='sandboxapp.cloopen.com';

    //请求端口
    private $serverPort='8883';

    //REST版本号
    private $softVersion='2013-12-26';

    /**
     * 发送模板短信
     * @param to 手机号码集合,用英文逗号分开
     * @param datas 内容数据 格式为数组 例如：array('Marry','Alon')，如不需替换请填 null
     * @param $tempId 模板Id
     */
    public function sendTemplateSMS($to,$datas,$tempId)
    {
        // 初始化REST SDK
        $rest = new CCPRestSmsSDK($this->serverIP,$this->serverPort,$this->softVersion);
        $rest->setAccount($this->accountSid,$this->accountToken);
        $rest->setAppId($this->appId);



        // 发送模板短信
        //echo "Sending TemplateSMS to $to <br/>";
        $result = $rest->sendTemplateSMS($to,$datas,$tempId);
        if($result == NULL ) {
            //echo "result error!";

        }
        if($result->statusCode!=0) {

            echo "error code :" . $result->statusCode . "<br>";
            echo "error msg :" . $result->statusMsg . "<br>";
            //TODO 添加错误处理逻辑
        }else{
           // echo "Sendind TemplateSMS success!<br/>";
            // 获取返回信息
            $smsmessage = $result->TemplateSMS;
            //echo "dateCreated:".$smsmessage->dateCreated."<br/>";
            //echo "smsMessageSid:".$smsmessage->smsMessageSid."<br/>";
            //TODO 添加成功处理逻辑
        }
    }
}

//sendTemplateSMS("18576437523", array(1234, 5), 1);
